package org.example.blog_user_service.Service;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import org.example.blog_user_service.mapper.AdminMapper;
import org.example.blog_user_service.mapper.UserMapper;
import org.example.blog_user_service.model.Admin;
import org.example.blog_user_service.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;

/**
 * author: 和亿宝
 * time：2025/3/14 21:59
 * version:v1.0
 */
@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private PasswordEncoder passwordEncoder;

    public ResponseEntity<?> registerUser(User user) {
        if (userMapper.selectByLoginName(user.getLoginName()) != null) {
            return ResponseEntity.badRequest().body("Login name already exists");
        }
        user.setPassword(passwordEncoder.encode(user.getPassword()));
        user.setStatus("PENDING");
        int result = userMapper.insertUser(user);
        if (result > 0) {
            return ResponseEntity.ok().body("Registration successful");
        } else {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Registration failed");
        }
    }

    public ResponseEntity<?> registerAdmin(Admin admin) {
        if (adminMapper.selectByLoginName(admin.getLoginName()) != null) {
            return ResponseEntity.badRequest().body("Login name already exists");
        }
        admin.setPassword(passwordEncoder.encode(admin.getPassword()));
        int result = adminMapper.insertAdmin(admin);
        if (result > 0) {
            return ResponseEntity.ok().body("Registration successful");
        } else {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Registration failed");
        }
    }

    public ResponseEntity<?> login(User user) {
        User existingUser = userMapper.selectByLoginName(user.getLoginName());
        if (existingUser == null) {
            return ResponseEntity.badRequest().body("Invalid login name or password");
        }
        if (!passwordEncoder.matches(user.getPassword(), existingUser.getPassword())) {
            return ResponseEntity.badRequest().body("Invalid login name or password");
        }
        String token = JWT.create()
                .withSubject(existingUser.getLoginName())
                .withExpiresAt(new Date(System.currentTimeMillis() + 86400000))
                .sign(Algorithm.HMAC256("your_jwt_secret_key"));
        return ResponseEntity.ok(Collections.singletonMap("token", token));
    }

    public ResponseEntity<?> login(Admin admin) {
        Admin existingAdmin = adminMapper.selectByLoginName(admin.getLoginName());
        if (existingAdmin == null) {
            return ResponseEntity.badRequest().body("Invalid login name or password");
        }
        if (!passwordEncoder.matches(admin.getPassword(), existingAdmin.getPassword())) {
            return ResponseEntity.badRequest().body("Invalid login name or password");
        }
        String token = JWT.create()
                .withSubject(existingAdmin.getLoginName())
                .withExpiresAt(new Date(System.currentTimeMillis() + 86400000))
                .sign(Algorithm.HMAC256("your_jwt_secret_key"));
        return ResponseEntity.ok(Collections.singletonMap("token", token));
    }
}
